package com.nuc.zyt.day04.demo.luckydraw;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName Lottery1
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/5/30 9:22
 * @Version 1.0
 */

public class Lottery1 extends Thread{
    static List<Integer> arr = Stream.of(100,50,200,800,1000,900,566,322,444,777,999).
            collect(Collectors.toList());

//    public Lottery1(List<Integer> list){
//        this.arr = list;
//    }

    @Override
    public void run() {
        // 开始抽奖
        while (true){
            synchronized (Lottery1.class){
//                // 随机实现 1 Random
//                if (!arr.isEmpty()){
//                    Integer integer = arr.get(new Random().nextInt(arr.size()));
//                    System.out.println(getName()+"又抽到了一个"+integer+"元大奖");
//                    arr.remove(integer);
//                }else break;

                // 2.
                if(arr.size() == 0){
                    break;
                }else {
                    Collections.shuffle(arr); // 打乱集合索引
                    Integer remove = arr.remove(0); // 删除0索引元素并返回
                    System.out.println(getName()+"又抽到了一个"+ remove+"元大奖");
                }

            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
